import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';

Page({

    data: {
        orders: [],
        token: "",
        orderSn: '',
        totalPrice: '',
        showModalDlg: false
    },

    onShow(options) {
        const token = wx.getStorageSync("token");
        if (!token) {
            wx.navigateTo({
                url: '/pages/login/login'
            });
            return;
        } else {
            this.setData({ token })
        }
        this.getOrders();
    },


    // 获取订单列表的方法
    async getOrders() {
        const res = await request({
            url: '/order/order/app/orderList',
            header: { "token": this.data.token }
        });
        this.setData({
            orders: res.data
        });
    },



    // 展示模态框
    showModel(e) {
        const { ordersn } = e.target.dataset;
        const { payamount } = e.target.dataset;
        this.setData({
            showModalDlg: true,
            orderSn: ordersn,
            totalPrice: payamount
        });
    },


    // 点击支付按钮
    async handleOrderPay() {
        // 6 发起 支付接口
        const orderSn = this.data.orderSn;
        const result = await request({
            url: "/order/app/payed/notify",
            data: { "orderSn": orderSn }
        });

        if (result.code == 0) {
            // 7 支付成功
            this.onShow();
        }
    },



    // 取消支付 跳转到订单页面
    cancelModal() {
        var that = this;
        wx.showModal({
            title: '取消支付',
            success(res) {
                if (res.confirm) {
                    that.setData({ showModalDlg: false });
                } else if (res.cancel) {
                    that.setData({ showModalDlg: true });
                }
            }
        });
    },

    async refund(e) {
        const { ordersn } = e.target.dataset;
        const result = await request({
            url: "/order/orderreturnapply/app/" + ordersn + "/return",
            header: { "token": this.data.token }
        });

        if (result.code == 0) {
            // 7 支付成功
            this.onShow();
        }
    }
})